import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'
import axios from "axios";

Vue.use(VueRouter)
import Login from "@/components/Login";
import UserCenter from "@/components/UserCenter";
import Collection from "@/components/Collection";
import HomePage from "@/components/HomePage";
import OnlineLab from "@/components/OnlineLab";
import Articles from "@/components/Articles";
import OnlineVideo from "@/components/OnlineVideo.vue";
import UserRanking from "@/components/UserRanking.vue";
import Overview from "@/components/Overview";
import PermissionList from "@/components/PermissionList";
import MangerHome from "@/components/MangerHome";
import ManagerList from "@/components/ManagerList";
import Detail from "@/components/Detail";
import History from "@/components/History";
import Register from "@/components/Register";
import UpdatePass from "@/components/UpdatePass";
import MangerRangerList from "@/components/MangerRangerList.vue";
import LoginBox from "@/components/LoginBox";
import Success from "@/components/Success";
import UpdatePhoneNumber from "@/components/UpdatePhoneNumber";

const routes = [
    //我们在这里配置自己的路由规则
    {path: "/Login", name: "Login", component: LoginBox},
    {path: "/Register", name: "Register", component: Register},
    {
        path: "/UserCenter", name: "UserCenter", component: UserCenter,
        children: [
            {path: "/", name: "Overview", component: Overview},
            {path: "/PermissionList", name: "PermissionList", component: PermissionList},
            {path: "/UpdatePass", name: "UpdatePass", component: UpdatePass},
            {path: "/Success", name: "Success", component: Success},
            {path: "/History", name: "History", component: History},
            {path: "/Collection", name: "Collection", component: Collection},
            {path: "/UpdatePhoneNumber", name: "UpdatePhoneNumber", component: UpdatePhoneNumber},
        ],
        beforeEnter: async (to, from, next) => {
            // 检查用户是否已登录，如果已登录则允许访问该路由，否则将用户重定向到登录页
            const isAuthenticated = await checkUserExistence();
            //如果是到登录页或者主頁,那就直接放行
            if (!isAuthenticated) {
                next('/login');
            }
            const userPermission = await getUserPermission();
            if (userPermission == 2) {
                next();
            } else {
                next('/MangerHome');
            }
        }
    },
    {
        path: "/MangerHome", name: "MangerHome", component: MangerHome,
        children: [
            {path: '', name: "ManagerList", component: ManagerList,
                beforeEnter: async (to, from, next) => {
                    const userPermission = await getUserPermission();
                    if (userPermission == 0) {
                        next();
                    } else {
                        next('/MangerRangerList');
                    }
                }
            },
            {path: "/MangerRangerList", name: "MangerRangerList", component: MangerRangerList},
        ],
        beforeEnter: async (to, from, next) => {
            // 检查用户是否已登录，如果已登录则允许访问该路由，否则将用户重定向到登录页
            const isAuthenticated = await checkUserExistence();
            // 如果是到登录页或者主页,那就直接放行
            if (!isAuthenticated) {
                return next('/login');
            }
            const userPermission = await getUserPermission();
            if (userPermission == 0 || userPermission == 1) {
                next();
            } else {
                next('/userCenter');
            }
        }
    },
    {
        path: '/',
        name: 'HomePage',
        component: HomePage,
        children: [
            {
                path: '',
                name: 'OnlineLab',
                component: OnlineLab
            },
            {
                path: '/articles',
                name: 'Articles',
                component: Articles
            },
            {
                path: '/onlineVideo',
                name: 'OnlineVideo',
                component: OnlineVideo
            },
            {
                path: '/userRanking',
                name: 'UserRanking',
                component: UserRanking
            },
            {
                path: '/Detail',
                name: 'Detail',
                component: Detail,
                beforeEnter: async (to, from, next) => {
                    // 检查用户是否已登录，如果已登录则允许访问该路由，否则将用户重定向到登录页
                    const isAuthenticated = await checkUserExistence();
                    //如果是到登录页或者主頁,那就直接放行
                    if (isAuthenticated) {
                        next();
                    } else {
                        next('/login');
                    }
                }
            },
        ]
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes

})

//检查登录状态
async function checkUserExistence() {
    const token = Cookies.get('user');

    if (!token) {
        //没有token,返回false
        return false;
    }
    //有token,認為已經是登錄狀態了,返回true
    return true;
}

//检查用户权限
async function getUserPermission() {
    const token = Cookies.get('user');
    const params = {
        token: token
    }
    return axios.post('/userinfo/analysis', params).then(res => {
        return res.data.data.permission;
    });
}

export default router
